package com.gxa.ssm251.controller;

import com.gxa.ssm251.exception.BusinessException;
import com.gxa.ssm251.pojo.CpScope;
import com.gxa.ssm251.pojo.Points;
import com.gxa.ssm251.pojo.User;
import com.gxa.ssm251.service.PointsService;
import com.gxa.ssm251.service.UserService;
import com.gxa.ssm251.util.JsonResult;
import com.gxa.ssm251.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 舒空
 * //@CrossOrigin 解决跨域
 */
@RestController
@RequestMapping("/points")
@Api(value = "积分控制器", tags = "积分接口api")
@CrossOrigin//解决跨域

public class PointsController {

    @Resource
    private PointsService pointsService;

    private JsonResult jsonResult = null;

    @ApiOperation("删除积分")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map",value = "json格式的积分id",paramType = "body")
    )
    @PostMapping("/delete")
    public JsonResult delete(@RequestBody Map<String, Object> map) {
        jsonResult = new JsonResult();
        Integer id = Integer.parseInt(map.get("id").toString());
        Integer delete = pointsService.delete(id);
        if (delete>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改积分")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map", value = "json格式的积分对象", paramType = "body")
    )
    @PostMapping("/update")
    public JsonResult updateCoupon(@RequestBody Map<String, Object> map) {
        jsonResult = new JsonResult();
        Integer rdcId = Integer.valueOf(map.get("id").toString());
        Points selectById = pointsService.selectById(rdcId);
        if (selectById != null) {
            jsonResult.setCode("1");
            jsonResult.setData(selectById);
        }

        Points points = new Points();
        points.setId(Integer.parseInt(map.get("id").toString()));
        points.setName(map.get("name").toString());
        points.setIssuing(map.get("issuing").toString());
        points.setItRvl(map.get("itRvl").toString());
        points.setAmount(Double.valueOf(map.get("amount").toString()));
        points.setUserId(Integer.parseInt(map.get("userId").toString()));
        Integer update = pointsService.update(points);
        if (update > 0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    @ApiOperation("分页逆序积分全查询")
    @PostMapping("/selectAllByPage")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map", value = "json格式的积分page和size")
    )
    public JsonResult selectAllByPage(@RequestBody Map<String, Object> map) {
        jsonResult = new JsonResult();
        int page = Integer.parseInt(map.get("page").toString());
        int size = Integer.parseInt(map.get("size").toString());
        page = (page - 1) * size;
        List<Points> coupons = pointsService.selectAllByPage(page, size);
        List<Points> selectPointsAll = pointsService.selectPointsAll();

        jsonResult.setCode("1");
        jsonResult.setCount(selectPointsAll.size());
        jsonResult.setData(coupons);
        return jsonResult;
    }

    @ApiOperation(value = "积分添加接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "map", value = "json格式的积分对象",
                    paramType = "body")
    )
    @PostMapping("/add")
    public JsonResult addPoints(@RequestBody Map<String, Object> map){
        jsonResult = new JsonResult();

        Points points = new Points();
        points.setName(map.get("name").toString());
        points.setName(map.get("name").toString());
        points.setIssuing(map.get("issuing").toString());
        points.setItRvl(map.get("itRvl").toString());
        points.setAmount(Double.valueOf(map.get("amount").toString()));
        Integer integer = pointsService.addPoints(points);

            if (integer > 0) {
                jsonResult.setCode("1");
                jsonResult.setMsg("添加成功");
            } else {
                jsonResult.setCode("0");
                jsonResult.setMsg("添加失败");
            }


        return jsonResult;
    }

}
